﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Gasco.Business.Services;
using Gasco.DataAccess;
using Mani.Common;

public partial class Controls_calculate_price : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadProducts();
            LoadPlattTypes();
            LoadOrder();
        }
    }

    private void LoadProducts()
    {
        ProductService ps = new ProductService();
        ddProducts.DataSource = ps.GetProducts();
        ddProducts.DataBind();
    }

    private void LoadPlattTypes()
    {
        LookupService<platts_type> ls = new LookupService<platts_type>();
        ddPlattsType.DataSource = ls.GetAll();
        ddPlattsType.DataBind();
    }

    private void LoadOrder()
    {
        if (Request.QueryString["orderid"] == null)
            return;

        OrderService os = new OrderService();
        LoadingService ls = new LoadingService();
        LookupService<unit> us = new LookupService<unit>();

        order o =  os.GetOrderById(Convert.ToInt32(Request.QueryString["orderid"]));
        txtOrderName.Text = o.order_name;
        txtOrderNum.Text = o.order_number;
        txtQuantity.Text = o.quantity.ToString();
        lbQuantityUnit.Text = us.FindById(o.unit_id).name;
        ddPlattsType.SelectedValue = o.platt_type_id.ToString();
        ddProducts.SelectedValue = o.product_id.ToString();

        IList<loading> loadings = ls.GetLoadingsByOrder(o.id);
        loading firstLoading = loadings.OrderBy(l => l.loading_date.Value).FirstOrDefault<loading>();
        loading lastLoading = loadings.OrderByDescending(l => l.offloading_date.Value).FirstOrDefault<loading>();

        if (firstLoading != null)
            txtStartDate.Text = firstLoading.loading_date.ToJalali();

        if (lastLoading != null)
            txtEndDate.Text = lastLoading.offloading_date.ToJalali();

        if (firstLoading != null && lastLoading != null)
        {
            PlattService ps = new PlattService();
            float averagePlatt = ps.GetAveragePriceByDate(o.product_id, o.platt_type_id, firstLoading.loading_date.Value, lastLoading.offloading_date.Value);
            txtPlattAverage.Text = UIHelper.FormatString(averagePlatt);
            txtPrice.Text = UIHelper.FormatString((averagePlatt * o.quantity));
        }
    }

    protected void btnclose_Click(object sender, EventArgs e)
    {
        if (Request.QueryString["orderid"] == null)
            return;

        OrderService os = new OrderService();
        order o = os.GetOrderById(Convert.ToInt32(Request.QueryString["orderid"]));
        o.price = Convert.ToSingle(UIHelper.RemoveFormat(txtPrice.Text));
        o.update_date = DateTime.Now;
        os.UpdateOrder(o);

        Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script1", "<script>pageClose();</script>");
    }
}